package com.ellecity06.cwhiskymall.adapter

import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.text.buildSpannedString
import androidx.core.text.scale
import com.ellecity06.common.base.BaseAdapter
import com.ellecity06.common.ktx.colorResToColorId
import com.ellecity06.common.ktx.load
import com.ellecity06.common.ktx.sp2px
import com.ellecity06.cwhiskymall.R
import com.ellecity06.cwhiskymall.bean.response.GoodResponse
import com.ellecity06.cwhiskymall.databinding.ItemNewerDetailGoodBinding
import com.ellecity06.cwhiskymall.storage.UserStorage
import com.ellecity06.cwhiskymall.utils.radiusBackground
import com.ellecity06.cwhiskymall.utils.textStyle

/**
 * @author ellecity06
 * @time 2024/11/13 15:50
 * @des
 */
class NewerDetailGoodAdapter :BaseAdapter<GoodResponse,ItemNewerDetailGoodBinding>() {
    override fun inflateBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ItemNewerDetailGoodBinding {
        return ItemNewerDetailGoodBinding.inflate(layoutInflater, parent, false)
    }

    override fun convert(binding: ItemNewerDetailGoodBinding, position: Int, item: GoodResponse) {
        binding.ivGoodImg.load(context, item.mainImage, 8F)
        binding.tvPrice.text = "￥${item.salesPrice}"
        binding.tvPrice.visibility = UserStorage.upVisible()
        if (UserStorage.isHasUp()){
            binding.tvContribution.text = buildSpannedString {
                scale(0.55F) {
                    append("赠送")
                }
                append(item.contribution)
                scale(0.55F) {
                    append("贡献金")
                }
            }
        }else {
            binding.tvContribution.text = "¥${item?.salesPrice}"
        }
        val nameSpan = buildSpannedString {
            if (item.goodsSource == 0) {
                radiusBackground(R.color.colorAccent.colorResToColorId(), 4) {
                    append("自营")
                }
            }

            textStyle(size = 15F.sp2px(), style = Typeface.BOLD) {
                append(" ${item.spuName}")
            }
        }
        binding.tvGoodsName.text = nameSpan
        binding.tvStock.text = "剩余库存：${item.spuStock}"

    }
}